import os
# from core.conf.config import settings
import sys

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)

from gooey import Gooey, GooeyParser


# @Gooey( program_name="Widget Demo")
@Gooey(optional_cols=2, program_name="HrunEx 执行引导程序",
       encoding="utf-8",  language='chinese')
def main():
    usage = """python hrunxls.py -i <input file> -x <excel file>
                                -r  <excel file> [-t [ALL|TestSuite|SheetSuite]]
                                -e -l [DEBUG|INFO|WARNING]"""
    desc = "HrunEx 执行引导程序"
    file_help_msg = "Name of the file you want to process"

    arg_parser = GooeyParser(description=desc)
    subs = arg_parser.add_subparsers(help='commands', dest='command')

    # 执行excel
    g_run = subs.add_parser('执行EXCEL', help='执行EXCEL')
    g_run.add_argument("runexcel", help='请选择Excel文件', metavar='Excel文件',
                       widget="FileChooser", gooey_options={
        'wildcard':
            "yaml file (*.xls)|*.xls|"
            "All files (*.*)|*.*",
        'default_dir': curPath,
        'message': "pick me"
    })
    # g_run.add_argument("-r", "--runexcel", dest="runexcel", help="run hrun excel testcase")
    g_run.add_argument("-t", "--type", dest="sheetype", choices=['Sheet1', '-', 'TestSuite', 'SheetSuite', 'All'],
                       help="sheets type: All|TestSuite|SheetSuite",
                       default='-')

    # 执行选项
    g_run.add_argument("-e", "--extendreport", action="store_true", dest="extendreport",
                       help="Extend Report teamplate or default template ", default=False)
    g_run.add_argument("-l", "--loglevel", choices=['WARNING', 'INFO', 'DEBUG'], dest="loglevel",
                       help="loglevel  DEBUG, INFO, WARNING", default="DEBUG")
    g_run.add_argument("-p", "--reporturlpath", dest="reporturlpath", help="report url path ",
                       default="")

    # 转换 为 excel
    g_trans = subs.add_parser('文件转换', help='文件转换 将文件(yml/json/har)转换为 Excel ')
    g_trans.add_argument("infile", help='请选择待转换文件, file(yml/json/har)', widget="FileChooser",
                         metavar='输入文件',
                         gooey_options={
        'wildcard':
            "HAR file (*.har)|*.har|"
            "Swagger Json file (*.json)|*.json|"
            "yaml file (*.yml)|*.csv|"
            "All files (*.*)|*.*",
        'default_dir': curPath,
        'message': "pick me"
    })
    # g_trans.add_argument("-i", "--infile", dest="infile", help="input file(yml/json/har)")
    g_trans.add_argument("-x", "--toexcel", dest="toexcel", help="file(yml/json/har) to excel file")

    args = arg_parser.parse_args()

    # print(sys.argv[1:])
    print(args)
    if args.command == '文件转换':
        print('start 文件转换')

        infile = ''
        if args.infile:  # and "har" in self.options.testcase:
            infile = args.infile

        toexcel = args.toexcel if args.toexcel \
            else "%s.xls" % ".".join((os.path.basename(infile).split('.')[0:-1]))

        # todo 指定excel路径
        toexcel = os.path.join(os.path.dirname(infile), toexcel)

        print(infile + '  ' + toexcel)

        from hruntools.file2xls import loadfile2excel
        loadfile2excel(infile, toexcel)
    elif args.command == '执行EXCEL':
        from hruntools.call_xls import call_excel
        print('start 执行Excel')
        excelfile = args.runexcel
        print(excelfile)
        sheeType = '' if args.sheetype == '-' else args.sheetype

        # return
        call_excel(excelfile, loglevel=args.loglevel, isExtendReport=args.extendreport,
                   reportURL=args.reporturlpath,
                   sheetType=sheeType)


        # if args.sheetype:
        #     # 多sheet 测试，按照sheetype 控制
        #     call_excel(excelfile, loglevel=args.loglevel, isExtendReport=args.extendreport,
        #                reportURL=args.reporturlpath,
        #                sheetType=sheeType)
        # else:
        #     # 默认执行单 sheet 测试用例
        #     call_excel(excelfile, loglevel=args.loglevel, isExtendReport=args.extendreport,
        #                reportURL=args.reporturlpath)


def toExcelValue():
    pass


if __name__ == '__main__':
    main()
